{extend name="$_admin_public_base_"/}
  {block name="style"}
    <!--此页面增加的style-->
  {/block}
{block name="main"}
<section class="content pt-5">
<div class="box box-solid eacoo-box">
    <div class="box-body">
        <div class="cf eacoo-tabs">
            <ul class="nav nav-tabs">
                <li class="active"><a href="{:url('admin/Database/index',['type'=>'export'])}">数据备份</a></li>
                 <li class=""><a href="{:url('admin/Database/index',['type'=>'import'])}">数据恢复</a></li>
            </ul>
            <div class="mt-10">
                <a id="export" class="btn btn-primary" href="javascript:;" autocomplete="off"><i class="fa fa-download"></i> 立即备份</a>
                <a id="optimize" class="btn btn-primary" href="{:url('optimize')}"><i class="fa fa-shield"></i> 优化表</a>
                <a id="repair" class="btn btn-primary" href="{:url('repair')}"><i class="fa fa-wrench"></i> 修复表</a>
            </div>
        </div>

        <!-- 应用列表 -->
        <div class="data-table table-striped">
            <form id="export-form" method="post" action="{:url('export')}">
                <table class="table table-hover table-responsive">
                    <thead>
                        <tr>
                            <th width="48" class="checkbox-toggle"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                            <th>表名</th>
                            <th width="120">引擎</th>
                            <th width="120">编码</th>
                            <th width="120">记录数</th>                      
                            <th width="120">大小</th>
                            <th width="120">备注</th>
                            <th width="120">状态</th>
                            <th width="120">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {volist name="list" id="table"}
                            <tr>
                                <td class="num">
                                    <input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                                </td>
                                <td>{$table.name}</td>
                                <td>{$table.engine}</td>
                                <td>{$table.collation}</td>
                                <td>{$table.rows}</td>
                                <td>{$table.data_length|format_bytes}</td>
                                <td>{$table.comment}</td>
                                <td class="info">未备份</td>
                                <td class="action">
                                    <a class="ajax-get no-refresh" href="{:url('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                                    <a class="ajax-get no-refresh" href="{:url('repair?tables='.$table['name'])}">修复表</a>
                                </td>
                            </tr>
                        {/volist}
                    </tbody>
                </table>
            </form>
        </div>
        <!-- /应用列表 -->
    </div>
</div>
</section>
{/block}

{block name="script"}
<script type="text/javascript">
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.code){
                    updateAlert(data.msg,'success');
                } else {
                    updateAlert(data.msg,'error');
                }
                setTimeout(function(){
	                //$('#top-alert').find('button').click();
	                //$(that).removeClass('disabled').prop('disabled',false);
	            },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.code==1){
                        var info = data.data;
                        tables = info.tables;
                        $export.html(data.msg + "开始备份，请不要关闭本页面！");
                        backup(info.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        updateAlert(data.msg,'error');
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
        	                //$('#top-alert').find('button').click();
        	                //$(that).removeClass('disabled').prop('disabled',false);
        	            },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.code==1){
                    var info = data.data;
                    showmsg(tab.id, data.msg);

                    if(!$.isPlainObject(info.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(info.tab, tab.id != info.tab.id);
                } else {
                    updateAlert(data.msg,'error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
    	                //$('#top-alert').find('button').click();
    	                //$(that).removeClass('disabled').prop('disabled',false);
    	            },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){console.debug(tables);
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }

    })(jQuery);
</script>
{/block}
